﻿using Dapper;

using HBase;
using hCommand;
using System;
using System.Collections.Generic;
using System.Linq;
using static Dapper.SqlMapper;

namespace Persistence {
    public class AbsDoServer : AbsExtendsServer, IDoServer
    { 

        public List<T> getDetials<T>(long pid) where T : BaseEntity { 
            return readConn().GetAll<T>(" and pid=@pid", new { pid }) ;
        }
        public void finshDo<T>(T dto) where T : AbsDo {
            dto.status = 9;
            dto.finshTime= DateTime.Now;
            opConn().Update(dto, x=>new { x.status, x.finshTime } );
        }

   

       
        public Page<WhDoStat> queryStat(PDoStatDto dto) {
            string sql = @" SELECT  B.* FROM    Wh_Do_Stat B   where 1=1  ";

            if ( dto.itemId>0) {
                sql += "and item_Id=@itemId ";
            }
            if (!String.IsNullOrEmpty(dto.batchNo)) {
                dto.batchNo = "%" + dto.batchNo + "%";
                sql += "and batch_No like @batchNo ";
            }

            if (dto.b.Ticks > 0) {
                sql += "and  create_Time > @b ";
            }
            if (dto.e.Ticks > 0) {
                sql += "and  create_Time < @e ";
            }

            return queryPage<WhDoStat>(sql, " create_time  desc", dto);
        }
        public List<WhDoStat> getStat(PDoStatDto dto) {

            String where = " where 1=1 ";
            if (dto.itemId > 0) {
                where += " and item_Id=@itemId ";
            }
            if (dto.b.Ticks > 0) {
                where += " and  create_Time >= @b ";
            }
            if (dto.e.Ticks > 0) {
                dto.e=dto.e.AddDays(1);   
                where += " and  create_Time <= @e ";
            }


            string sql = $"select * from Wh_Do_Stat {where} ";
        

                return readConn().Query<WhDoStat>(sql, dto).ToList();
           
    

        }

    }
}
